package com.atguigu04.inputstreamreader.exer;

import com.sun.source.tree.WhileLoopTree;
import org.junit.Test;

import java.io.*;

/**
 * ClassName: InputStreamReaderTest
 * Package: com.atguigu04.inputstreamreader.exer
 * Description:
 *
 * @Author BuTang
 * @Create 2023/6/15 18:05
 * @Version 1.0
 */
public class InputStreamReaderTest {


    /**
     *
     把当前module下的《康师傅的话.txt》字符编码为GBK，复制到电脑桌面目录下的《寄语.txt》，
     字符编码为UTF-8。
     */

    @Test
    public void test1(){

        InputStreamReader inputStreamReader = null;
        OutputStreamWriter outputStreamWriter = null;

        try {

            FileInputStream oldFile = new FileInputStream("康师傅的话.txt");
            FileOutputStream newFile = new FileOutputStream("C:\\Users\\11921\\Desktop\\寄语.txt");

            inputStreamReader = new InputStreamReader(oldFile,"GBK");
            outputStreamWriter = new OutputStreamWriter(newFile,"UTF-8");

            char[] buffer = new char[5];
            int len ;

            //需要注意，我经常写的时候忘记read里将buffer放入
            while ((len = inputStreamReader.read(buffer)) != -1){
                outputStreamWriter.write(buffer,0,len);
                outputStreamWriter.flush();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (inputStreamReader != null)
                    inputStreamReader.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                if (outputStreamWriter != null)
                    outputStreamWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }


    }


}
